Imports System
Imports System.Data
Imports System.Collections
Imports System.Collections.Generic

Imports Framework

<Serializable(), CLSCompliant(True)> _
Partial Public Class CConnection
    Inherits CBaseDynamic
    Implements IComparable(Of CConnection)

#Region "Constructors"
    'Public (Copy Constructor)
    Public Sub New(ByVal original as CConnection, target As CDataSrc)
        m_dataSrc = target
        m_connectionSetId = original.ConnectionSetId
        m_connectionName = original.ConnectionName
        m_connectionUserName = original.ConnectionUserName
        m_connectionDriverId = original.ConnectionDriverId
        m_connectionString = original.ConnectionString
        m_connectionPassword = original.ConnectionPassword
        m_connectionOutputDir = original.ConnectionOutputDir
        m_connectionNameSpace = original.ConnectionNameSpace
        m_connectionUseAuditTrail = original.ConnectionUseAuditTrail
        m_connectionUseCaching = original.ConnectionUseCaching
        m_connectionTablePrefix = original.ConnectionTablePrefix
        m_connectionCreated = original.ConnectionCreated
    End Sub

    'Protected (Datareader/Dataset)
    Protected Sub New(ByVal dataSrc As CDataSrc, ByVal dr As IDataReader)
        MyBase.New(dataSrc, dr)
    End Sub
    Protected Sub New(ByVal dataSrc As CDataSrc, ByVal dr As DataRow)
        MyBase.New(dataSrc, dr)
    End Sub
#End Region

#Region "Default Values"
    Protected Overrides Sub InitValues_Auto()
        'Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
        m_connectionId = Integer.MinValue
        m_connectionSetId = Integer.MinValue
        m_connectionName = String.Empty
        m_connectionUserName = String.Empty
        m_connectionDriverId = Integer.MinValue
        m_connectionString = String.Empty
        m_connectionPassword = String.Empty
        m_connectionOutputDir = String.Empty
        m_connectionNameSpace = String.Empty
        m_connectionUseAuditTrail = False
        m_connectionUseCaching = False
        m_connectionTablePrefix = String.Empty
        m_connectionCreated = DateTime.MinValue
    End Sub
#End Region

#Region "Members"
    Protected m_connectionId As Integer
    Protected m_connectionSetId As Integer
    Protected m_connectionName As String
    Protected m_connectionUserName As String
    Protected m_connectionDriverId As Integer
    Protected m_connectionString As String
    Protected m_connectionPassword As String
    Protected m_connectionOutputDir As String
    Protected m_connectionNameSpace As String
    Protected m_connectionUseAuditTrail As Boolean
    Protected m_connectionUseCaching As Boolean
    Protected m_connectionTablePrefix As String
    Protected m_connectionCreated As DateTime
#End Region

#Region "Properties - Column Values"
    'Primary Key Column (ReadOnly)
    Public ReadOnly Property [ConnectionId]() As Integer
        Get
            Return m_connectionId
        End Get
    End Property

    'Table Columns (Read/Write)
    Public Property [ConnectionSetId]() As Integer
        Get
            Return m_connectionSetId
        End Get
        Set(ByVal value As Integer)
            m_connectionSetId = value
        End Set
    End Property
    Public Property [ConnectionName]() As String
        Get
            Return m_connectionName
        End Get
        Set(ByVal value As String)
            m_connectionName = value
        End Set
    End Property
    Public Property [ConnectionUserName]() As String
        Get
            Return m_connectionUserName
        End Get
        Set(ByVal value As String)
            m_connectionUserName = value
        End Set
    End Property
    Public Property [ConnectionDriverId]() As Integer
        Get
            Return m_connectionDriverId
        End Get
        Set(ByVal value As Integer)
            m_connectionDriverId = value
        End Set
    End Property
    Public Property [ConnectionString]() As String
        Get
            Return m_connectionString
        End Get
        Set(ByVal value As String)
            m_connectionString = value
        End Set
    End Property
    Public Property [ConnectionPassword]() As String
        Get
            Return m_connectionPassword
        End Get
        Set(ByVal value As String)
            m_connectionPassword = value
        End Set
    End Property
    Public Property [ConnectionOutputDir]() As String
        Get
            Return m_connectionOutputDir
        End Get
        Set(ByVal value As String)
            m_connectionOutputDir = value
        End Set
    End Property
    Public Property [ConnectionNameSpace]() As String
        Get
            Return m_connectionNameSpace
        End Get
        Set(ByVal value As String)
            m_connectionNameSpace = value
        End Set
    End Property
    Public Property [ConnectionUseAuditTrail]() As Boolean
        Get
            Return m_connectionUseAuditTrail
        End Get
        Set(ByVal value As Boolean)
            m_connectionUseAuditTrail = value
        End Set
    End Property
    Public Property [ConnectionUseCaching]() As Boolean
        Get
            Return m_connectionUseCaching
        End Get
        Set(ByVal value As Boolean)
            m_connectionUseCaching = value
        End Set
    End Property
    Public Property [ConnectionTablePrefix]() As String
        Get
            Return m_connectionTablePrefix
        End Get
        Set(ByVal value As String)
            m_connectionTablePrefix = value
        End Set
    End Property
    Public Property [ConnectionCreated]() As DateTime
        Get
            Return m_connectionCreated
        End Get
        Set(ByVal value As DateTime)
            m_connectionCreated = value
        End Set
    End Property

    'View Columns (ReadOnly)

#End Region

#Region "MustOverride Methods"
    'Schema Information
    Public Const TABLE_NAME As String = "CodeGen_Connections"
    Public Const VIEW_NAME As String  = ""          'Used to override Me.ViewName
    Public Const ORDER_BY_COLS As String = "ConnectionUserName, ConnectionName" 'See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
    Public Const SORTING_COLUMN As String = ""
    Public Overrides ReadOnly Property TableName() As String
        Get
            Return TABLE_NAME
        End Get
    End Property
    Protected Overrides ReadOnly Property OrderByColumns() As String
        Get
            Return ORDER_BY_COLS
        End Get
    End Property

    'CompareTo Interface (Default Sort Order)
    Public Function CompareTo(ByVal other As CConnection) As Integer Implements IComparable(Of CConnection).CompareTo
        Dim i As Integer = Me.ConnectionUserName.CompareTo(other.ConnectionUserName) 
        If 0 <> i Then Return i
        Return Me.ConnectionName.CompareTo(other.ConnectionName) 
    End Function

    'Primary Key Information
    Public Const PRIMARY_KEY_NAME As String = "ConnectionId"
    Protected Overrides ReadOnly Property InsertPrimaryKey() As Boolean
        Get
            Return False
        End Get
    End Property
    Protected Overrides ReadOnly Property PrimaryKeyName() As String
        Get
            Return PRIMARY_KEY_NAME
        End Get
    End Property
    Protected Overrides Property PrimaryKeyValue() As Object
        Get
            Return m_connectionId
        End Get
        Set(ByVal value As Object)
            m_connectionId = CType(Value, Integer)
        End Set
    End Property

    'Factory Methods - Object
    Protected Overrides Function MakeFrom(ByVal row As DataRow) As CBase
        Return New CConnection(Me.DataSrc, row)
    End Function
    Protected Overrides Function MakeFrom(ByVal dr As IDataReader) As CBase
        Return New CConnection(Me.DataSrc, dr)
    End Function
    
    'Factory Methods - List
    Protected Overrides Function MakeList() As IList
        Return New CConnectionList
    End Function
    Protected Overrides Function MakeList(ByVal capacity As Integer) As IList
        Return New CConnectionList(capacity)
    End Function

    'Convert from ADO to .Net
    Protected Overrides Sub ReadColumns(ByVal dr As IDataReader)
        m_connectionId = CAdoData.GetInt(dr, "ConnectionId")
        m_connectionSetId = CAdoData.GetInt(dr, "ConnectionSetId")
        m_connectionName = CAdoData.GetStr(dr, "ConnectionName")
        m_connectionUserName = CAdoData.GetStr(dr, "ConnectionUserName")
        m_connectionDriverId = CAdoData.GetInt(dr, "ConnectionDriverId")
        m_connectionString = CAdoData.GetStr(dr, "ConnectionString")
        m_connectionPassword = CAdoData.GetStr(dr, "ConnectionPassword")
        m_connectionOutputDir = CAdoData.GetStr(dr, "ConnectionOutputDir")
        m_connectionNameSpace = CAdoData.GetStr(dr, "ConnectionNameSpace")
        m_connectionUseAuditTrail = CAdoData.GetBool(dr, "ConnectionUseAuditTrail")
        m_connectionUseCaching = CAdoData.GetBool(dr, "ConnectionUseCaching")
        m_connectionTablePrefix = CAdoData.GetStr(dr, "ConnectionTablePrefix")
        m_connectionCreated = CAdoData.GetDate(dr, "ConnectionCreated")
    End Sub
    Protected Overrides Sub ReadColumns(ByVal dr As DataRow)
        m_connectionId = CAdoData.GetInt(dr, "ConnectionId")
        m_connectionSetId = CAdoData.GetInt(dr, "ConnectionSetId")
        m_connectionName = CAdoData.GetStr(dr, "ConnectionName")
        m_connectionUserName = CAdoData.GetStr(dr, "ConnectionUserName")
        m_connectionDriverId = CAdoData.GetInt(dr, "ConnectionDriverId")
        m_connectionString = CAdoData.GetStr(dr, "ConnectionString")
        m_connectionPassword = CAdoData.GetStr(dr, "ConnectionPassword")
        m_connectionOutputDir = CAdoData.GetStr(dr, "ConnectionOutputDir")
        m_connectionNameSpace = CAdoData.GetStr(dr, "ConnectionNameSpace")
        m_connectionUseAuditTrail = CAdoData.GetBool(dr, "ConnectionUseAuditTrail")
        m_connectionUseCaching = CAdoData.GetBool(dr, "ConnectionUseCaching")
        m_connectionTablePrefix = CAdoData.GetStr(dr, "ConnectionTablePrefix")
        m_connectionCreated = CAdoData.GetDate(dr, "ConnectionCreated")
    End Sub

    'Parameters for Insert/Update    
    Protected Overrides Function ColumnNameValues() As CNameValueList
        Dim data As New CNameValueList
        data.Add("ConnectionId", NullVal(m_connectionId))
        data.Add("ConnectionSetId", NullVal(m_connectionSetId))
        data.Add("ConnectionName", NullVal(m_connectionName))
        data.Add("ConnectionUserName", NullVal(m_connectionUserName))
        data.Add("ConnectionDriverId", NullVal(m_connectionDriverId))
        data.Add("ConnectionString", NullVal(m_connectionString))
        data.Add("ConnectionPassword", NullVal(m_connectionPassword))
        data.Add("ConnectionOutputDir", NullVal(m_connectionOutputDir))
        data.Add("ConnectionNameSpace", NullVal(m_connectionNameSpace))
        data.Add("ConnectionUseAuditTrail", NullVal(m_connectionUseAuditTrail))
        data.Add("ConnectionUseCaching", NullVal(m_connectionUseCaching))
        data.Add("ConnectionTablePrefix", NullVal(m_connectionTablePrefix))
        data.Add("ConnectionCreated", NullVal(m_connectionCreated))
        Return data
    End Function
#End Region

#Region "Queries - SelectAll/SelectWhere (inherited methods, cast only)"
    'Select Queries - Non-Paged
    Public Shadows Function SelectAll() As CConnectionList
        Return CType(MyBase.SelectAll(), CConnectionList)
    End Function
    Public Shadows Function SelectAll(ByVal orderBy As String) As CConnectionList
        Return CType(MyBase.SelectAll(orderBy), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal colName As String, ByVal sign As ESign, ByVal colValue As Object) As CConnectionList
        Return CType(MyBase.SelectWhere(colName, sign, colValue), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal where As CCriteria) As CConnectionList
        Return CType(MyBase.SelectWhere(where), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal where As CCriteriaList) As CConnectionList
        Return CType(MyBase.SelectWhere(where), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal where As CCriteriaList, ByVal tableNameOrJoinExpression As String) As CConnectionList
        Return CType(MyBase.SelectWhere(where, tableNameOrJoinExpression, Me.OrderByColumns), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal where As CCriteriaList, ByVal tableNameOrJoinExpression As String, ByVal orderBy As String) As CConnectionList
        Return CType(MyBase.SelectWhere(where, tableNameOrJoinExpression, orderBy), CConnectionList)
    End Function
    <Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")> _
    Protected Shadows Function SelectWhere(ByVal unsafeWhereClause As String) As CConnectionList
        Return CType(MyBase.SelectWhere(unsafeWhereClause), CConnectionList)
    End Function
    Protected Shadows Function SelectById(ByVal connectionId As Integer) As CConnectionList
        Return CType(MyBase.SelectById(connectionId), CConnectionList)
    End Function
    Protected Shadows Function SelectByIds(ByVal ids As List(Of Integer)) As CConnectionList
        Return CType(MyBase.SelectByIds(ids), CConnectionList)
    End Function

    'Select Queries - Paged
    Public Shadows Function SelectAll(ByVal pi As CPagingInfo) As CConnectionList
        Return CType(MyBase.SelectAll(pi), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal pi As CPagingInfo, ByVal colName As String, ByVal sign As ESign, ByVal colValue As Object) As CConnectionList
        Return CType(MyBase.SelectWhere(pi, colName, sign, colValue), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal pi As CPagingInfo, ByVal criteria As CCriteria) As CConnectionList
        Return CType(MyBase.SelectWhere(pi, criteria), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal pi As CPagingInfo, ByVal criteria As CCriteriaList) As CConnectionList
        Return CType(MyBase.SelectWhere(pi, criteria), CConnectionList)
    End Function
    Protected Shadows Function SelectWhere(ByVal pi As CPagingInfo, ByVal criteria As CCriteriaList, ByVal tableViewOrJoinExpr As String) As CConnectionList
        Return CType(MyBase.SelectWhere(pi, criteria, tableViewOrJoinExpr), CConnectionList)
    End Function
    Protected Shadows Function SelectByIds(ByVal pi As CPagingInfo, ByVal ids As List(Of Integer)) As CConnectionList
        Return CType(MyBase.SelectByIds(pi, ids), CConnectionList)
    End Function

    'Select Queries - Transactional (Friend-scoped for use in cascade-deletes)
    Friend Shadows Function SelectAll(ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectAll(tx), CConnectionList)
    End Function
    Friend Shadows Function SelectWhere(ByVal columnName As String, columnValue As Object, ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectWhere(columnName, columnValue, tx), CConnectionList)
    End Function
    Friend Shadows Function SelectWhere(ByVal columnName As String, sign As ESign, columnValue As Object, ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectWhere(columnName, sign, columnValue, tx), CConnectionList)
    End Function
    Friend Shadows Function SelectWhere(ByVal criteria As CCriteria, ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectWhere(criteria, tx), CConnectionList)
    End Function
    Friend Shadows Function SelectWhere(ByVal criteria As CCriteriaList, ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectWhere(criteria, tx), CConnectionList)
    End Function
    Friend Shadows Function SelectWhere(ByVal criteria As CCriteriaList, ByVal tableOrJoin As String, ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectWhere(criteria, tableOrJoin, tx), CConnectionList)
    End Function
    Friend Shadows Function SelectWhere(ByVal criteria As CCriteriaList, ByVal tableOrJoin As String, ByVal orderBy As String, ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectWhere(criteria, tableOrJoin, orderBy, tx), CConnectionList)
    End Function
    Friend Shadows Function SelectById(ByVal connectionId As Integer, ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectById(connectionId, tx), CConnectionList)
    End Function
    Friend Shadows Function SelectByIds(ByVal ids As List(Of Integer), ByVal tx As IDbTransaction) As CConnectionList
        Return CType(MyBase.SelectByIds(ids, tx), CConnectionList)
    End Function
    
    'Select Queries - Stored Procedures
    Protected Overloads Function MakeList(ByVal storedProcName As String, ByVal txOrNull As IDbTransaction) As CConnectionList
        Return CType(MyBase.MakeList(storedProcName,         txOrNull), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal storedProcName As String, ByVal params As Object(), ByVal txOrNull As IDbTransaction) As CConnectionList
        Return CType(MyBase.MakeList(storedProcName, params, txOrNull), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal storedProcName As String, ByVal params As CNameValueList, ByVal txOrNull As IDbTransaction) As CConnectionList
        Return CType(MyBase.MakeList(storedProcName, params, txOrNull), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal storedProcName As String, ByVal params As List(Of Object), ByVal txOrNull As IDbTransaction) As CConnectionList
        Return CType(MyBase.MakeList(storedProcName, params, txOrNull), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal storedProcName As String, ByVal param1 As Integer, ByVal txOrNull As IDbTransaction) As CConnectionList
        Return CType(MyBase.MakeList(storedProcName, param1, txOrNull), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal storedProcName As String, ByVal param1 As String, ByVal txOrNull As IDbTransaction) As CConnectionList
        Return CType(MyBase.MakeList(storedProcName, param1, txOrNull), CConnectionList)
    End Function

    'Query Results
    Protected Overloads Function MakeList(ByVal ds As DataSet) As CConnectionList
        Return CType(MyBase.MakeList(ds), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal dt As DataTable) As CConnectionList
        Return CType(MyBase.MakeList(dt), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal rows As DataRowCollection) As CConnectionList
        Return CType(MyBase.MakeList(rows), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal dr As IDataReader) As CConnectionList
        Return CType(MyBase.MakeList(dr), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal drOrDs As Object) As CConnectionList
        Return CType(MyBase.MakeList(drOrDs), CConnectionList)
    End Function
    Protected Overloads Function MakeList(ByVal gzip As Byte()) As CConnectionList
        Return CType(MyBase.MakeList(gzip), CConnectionList)
    End Function
#End Region

#Region "Queries - SelectBy[FK] (user-nominated fk/bool columns)"
    'Note: These methods should only be used for cascade-deletes, or initialising the cache. Use CConnection.Cache.GetBy... for reqular queries
    
    'Non-Paged
    Protected Function SelectBySetId(ByVal connectionSetId As Integer) As CConnectionList
        Return SelectWhere(new CCriteriaList("ConnectionSetId", connectionSetId))
    End Function
    Protected Function SelectByUserName(ByVal connectionUserName As String) As CConnectionList
        Return SelectWhere(new CCriteriaList("ConnectionUserName", connectionUserName))
    End Function
    Protected Function SelectByDriverId(ByVal connectionDriverId As Integer) As CConnectionList
        Return SelectWhere(new CCriteriaList("ConnectionDriverId", connectionDriverId))
    End Function
    Protected Function SelectByUseAuditTrail(ByVal connectionUseAuditTrail As Boolean) As CConnectionList
        Return SelectWhere(new CCriteriaList("ConnectionUseAuditTrail", connectionUseAuditTrail))
    End Function
    Protected Function SelectByUseCaching(ByVal connectionUseCaching As Boolean) As CConnectionList
        Return SelectWhere(new CCriteriaList("ConnectionUseCaching", connectionUseCaching))
    End Function

    'Paged
    Protected Function SelectBySetId(pi as CPagingInfo, ByVal connectionSetId As Integer) As CConnectionList
        Return SelectWhere(pi, New CCriteriaList("ConnectionSetId", connectionSetId))
    End Function
    Protected Function SelectByUserName(pi as CPagingInfo, ByVal connectionUserName As String) As CConnectionList
        Return SelectWhere(pi, New CCriteriaList("ConnectionUserName", connectionUserName))
    End Function
    Protected Function SelectByDriverId(pi as CPagingInfo, ByVal connectionDriverId As Integer) As CConnectionList
        Return SelectWhere(pi, New CCriteriaList("ConnectionDriverId", connectionDriverId))
    End Function
    Protected Function SelectByUseAuditTrail(pi as CPagingInfo, ByVal connectionUseAuditTrail As Boolean) As CConnectionList
        Return SelectWhere(pi, New CCriteriaList("ConnectionUseAuditTrail", connectionUseAuditTrail))
    End Function
    Protected Function SelectByUseCaching(pi as CPagingInfo, ByVal connectionUseCaching As Boolean) As CConnectionList
        Return SelectWhere(pi, New CCriteriaList("ConnectionUseCaching", connectionUseCaching))
    End Function

    'Count
    Protected Function SelectCountBySetId(ByVal connectionSetId As Integer) As Integer
        Return SelectCount(New CCriteriaList("ConnectionSetId", connectionSetId))
    End Function
    Protected Function SelectCountByUserName(ByVal connectionUserName As String) As Integer
        Return SelectCount(New CCriteriaList("ConnectionUserName", connectionUserName))
    End Function
    Protected Function SelectCountByDriverId(ByVal connectionDriverId As Integer) As Integer
        Return SelectCount(New CCriteriaList("ConnectionDriverId", connectionDriverId))
    End Function
    Protected Function SelectCountByUseAuditTrail(ByVal connectionUseAuditTrail As Boolean) As Integer
        Return SelectCount(New CCriteriaList("ConnectionUseAuditTrail", connectionUseAuditTrail))
    End Function
    Protected Function SelectCountByUseCaching(ByVal connectionUseCaching As Boolean) As Integer
        Return SelectCount(New CCriteriaList("ConnectionUseCaching", connectionUseCaching))
    End Function

    'Transactional
    Friend Function SelectBySetId(ByVal connectionSetId As Integer, tx As IDbTransaction) As CConnectionList
        Return SelectWhere(New CCriteriaList("ConnectionSetId", connectionSetId), tx)
    End Function
    Friend Function SelectByUserName(ByVal connectionUserName As String, tx As IDbTransaction) As CConnectionList
        Return SelectWhere(New CCriteriaList("ConnectionUserName", connectionUserName), tx)
    End Function
    Friend Function SelectByDriverId(ByVal connectionDriverId As Integer, tx As IDbTransaction) As CConnectionList
        Return SelectWhere(New CCriteriaList("ConnectionDriverId", connectionDriverId), tx)
    End Function
    Friend Function SelectByUseAuditTrail(ByVal connectionUseAuditTrail As Boolean, tx As IDbTransaction) As CConnectionList
        Return SelectWhere(New CCriteriaList("ConnectionUseAuditTrail", connectionUseAuditTrail), tx)
    End Function
    Friend Function SelectByUseCaching(ByVal connectionUseCaching As Boolean, tx As IDbTransaction) As CConnectionList
        Return SelectWhere(New CCriteriaList("ConnectionUseCaching", connectionUseCaching), tx)
    End Function
#End Region

#Region "Static - Cache Implementation"
    Public Shared Property Cache() As CConnectionList
        Get
            Cache = CType(CCache.Get(CACHE_KEY), CConnectionList)
            If IsNothing(cache) Then
                SyncLock (CACHE_KEY)
                    Cache = CType(CCache.Get(CACHE_KEY), CConnectionList)
                    If IsNothing(cache) Then
                        Cache = LoadCache()
                        CConnection.Cache = Cache
                    End If
                End SyncLock
            End If
            Return cache
        End Get
        Set(ByVal value As CConnectionList)
            SetCache(value)
        End Set
    End Property

    'Change Management:
    'Clone, modify, and then replace the cache (threadsafe for anything iterating the collection)
    'Note that internal indices are dicarded each time unless handled in constructor and add/remove overrides
    Protected Overrides Sub CacheDelete()
        If CacheIsNull Then Exit Sub
        SyncLock (CACHE_KEY)
            Dim temp As CConnectionList = New CConnectionList(Cache)
            Dim size As Integer = temp.Count
            temp.Remove(Me)
            If size = temp.Count Then 'Remove might fail If cache was refreshed with New instances. Use old index                    
                temp.Remove(CacheGetById(temp))
            End If
            SetCache(temp)
        End SyncLock
    End Sub
    Protected Overrides Sub CacheInsert()
        If CacheIsNull Then Exit Sub
        SyncLock (CACHE_KEY)
            Dim temp As CConnectionList = New CConnectionList(Cache)
            temp.Add(Me)
            SetCache(temp)
        End SyncLock
    End Sub
    Protected Overrides Sub CacheUpdate()
        If CacheIsNull Then Exit Sub
        SyncLock (CACHE_KEY)
            Dim temp As CConnectionList = New CConnectionList(Cache)
            If Not temp.Contains(Me) Then
                temp.Remove(CacheGetById(temp))
                temp.Add(Me)
            End If
            SetCache(temp)
        End SyncLock
    End Sub
    Protected Overrides Sub CacheClear()
        Cache = Nothing
    End Sub
    Public Shared ReadOnly Property CacheIsNull() As Boolean
        Get
            Return IsNothing(CCache.Get(CACHE_KEY))
        End Get
    End Property
#End Region

#Region "ToXml"
    Protected Overrides Sub ToXml_Autogenerated(ByVal w As System.Xml.XmlWriter)
        Store(w, "ConnectionId", Me.ConnectionId)
        Store(w, "ConnectionSetId", Me.ConnectionSetId)
        Store(w, "ConnectionName", Me.ConnectionName)
        Store(w, "ConnectionUserName", Me.ConnectionUserName)
        Store(w, "ConnectionDriverId", Me.ConnectionDriverId)
        Store(w, "ConnectionString", Me.ConnectionString)
        Store(w, "ConnectionPassword", Me.ConnectionPassword)
        Store(w, "ConnectionOutputDir", Me.ConnectionOutputDir)
        Store(w, "ConnectionNameSpace", Me.ConnectionNameSpace)
        Store(w, "ConnectionUseAuditTrail", Me.ConnectionUseAuditTrail)
        Store(w, "ConnectionUseCaching", Me.ConnectionUseCaching)
        Store(w, "ConnectionTablePrefix", Me.ConnectionTablePrefix)
        Store(w, "ConnectionCreated", Me.ConnectionCreated)
    End Sub
#End Region


End Class